package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.Department;
import com.ncst.backend.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科室控制器
 */
@Api(tags = "科室管理")
@Slf4j
@RestController
@RequestMapping("/departments")
public class DepartmentController {
    
    @Autowired
    private DepartmentService departmentService;
    
    @ApiOperation("根据ID查询科室")
    @GetMapping("/{id}")
    public Result<Department> getById(@ApiParam("科室ID") @PathVariable Integer id) {
        Department department = departmentService.getById(id);
        return department != null ? Result.success(department) : Result.notFound("科室不存在");
    }
    
    @ApiOperation("查询所有科室")
    @GetMapping
    public Result<List<Department>> getAll() {
        return Result.success(departmentService.getAll());
    }
    
    @ApiOperation("根据条件查询科室列表")
    @PostMapping("/search")
    public Result<List<Department>> getByCondition(@ApiParam("查询条件") @RequestBody Department department) {
        return Result.success(departmentService.getByCondition(department));
    }
    
    @ApiOperation("分页查询科室")
    @GetMapping("/page")
    public Result<List<Department>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(departmentService.getByPage(pageNum, pageSize));
    }
    
    @ApiOperation("统计科室总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        return Result.success(departmentService.countAll());
    }
    
    @ApiOperation("根据条件统计科室数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody Department department) {
        return Result.success(departmentService.countByCondition(department));
    }
    
    @ApiOperation("新增科室")
    @PostMapping
    public Result<Void> save(@ApiParam("科室信息") @RequestBody Department department) {
        return departmentService.save(department) ? Result.success("新增成功") : Result.error("新增失败");
    }
    
    @ApiOperation("批量新增科室")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("科室列表") @RequestBody List<Department> list) {
        return departmentService.saveBatch(list) ? Result.success("批量新增成功") : Result.error("批量新增失败");
    }
    
    @ApiOperation("根据ID更新科室")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("科室ID") @PathVariable Integer id, @ApiParam("科室信息") @RequestBody Department department) {
        department.setId(id);
        return departmentService.updateById(department) ? Result.success("更新成功") : Result.error("更新失败");
    }
    
    @ApiOperation("根据ID删除科室")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("科室ID") @PathVariable Integer id) {
        return departmentService.removeById(id) ? Result.success("删除成功") : Result.error("删除失败");
    }
    
    @ApiOperation("批量删除科室")
    @DeleteMapping("/batch")
    public Result<Void> removeByIds(@ApiParam("科室ID列表") @RequestBody List<Integer> ids) {
        return departmentService.removeByIds(ids) ? Result.success("批量删除成功") : Result.error("批量删除失败");
    }
}
